package com.project.common.core.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wyy
 * @Date: 4/21/21 5:15 PM
 */
public class CollectionSortUtil {

    public static void main(String[] args) {
        List<Integer> targetList = new ArrayList<>();
        targetList.add(1);
        targetList.add(2);
        targetList.add(3);
        List<Integer> sourceList = new ArrayList<>();
        sourceList.add(4);
        sourceList.add(5);

        List<Integer> integers = mergeList(targetList, sourceList, 1, 1, 1);
        System.out.println(integers.toString());
    }

    /**
     * @param targetList     被插入集合
     * @param sourceList     插入集合
     * @param intervalNumber 间隔
     * @param stepSize       步长
     * @param startIndex     起始下标
     */
    public static List<Integer> mergeList(List<Integer> targetList, List<Integer> sourceList, Integer intervalNumber, Integer stepSize, Integer startIndex) {
        List<Integer> returnList = new ArrayList<>();
        int nextIndex = startIndex + intervalNumber - 1;
        for (int i = 0; i < targetList.size(); i++) {
            // 当前下标与插入下标相等，则开始插入步长数量的集合
            if (i == nextIndex) {
                // 先将当前章节插入队列
                Integer temp = targetList.get(i);
                returnList.add(temp);
                nextIndex += intervalNumber;

                // 插入步长数量源集合【源集合为0】
                if (sourceList.size() == 0) {
                    nextIndex = i + 1;
                    continue;
                }

                // 插入步长数量的源集合【源集合不为0，大于步长】
                if (sourceList.size() > stepSize) {
                    for (int j = 0; j < sourceList.size(); j++) {
                        if (j == stepSize) {
                            sourceList = sourceList.subList(stepSize, sourceList.size());
                            break;
                        }
                        Integer temp1 = sourceList.get(j);
                        returnList.add(temp1);
                    }

                    // 当前下标为最后一个，则插入所有的剩余数据
                    if (i == targetList.size() - 1) {
                        // 插入剩余章节
                        for (int j = 0; j < sourceList.size(); j++) {
                            Integer temp2 = sourceList.get(j);
                            returnList.add(temp2);
                        }
                        break;
                    }
                    continue;
                }

                // 插入步长数量的源集合数据【源集合不为0，小于步长】
                if (sourceList.size() <= stepSize && sourceList.size() != 0) {
                    // 插入新增课程
                    for (int j = 0; j < sourceList.size(); j++) {
                        Integer temp3 = sourceList.get(j);
                        returnList.add(temp3);
                    }
                    sourceList = new ArrayList<>();

                    // 插入剩余课程
                    nextIndex = i + 1;
                    continue;
                }
                continue;
            }

            // 当前下标小于插入下标，则插入当前数据
            if (i < nextIndex) {
                // 先将当前章节插入队列
                Integer temp4 = targetList.get(i);
                returnList.add(temp4);

                // 当前下标为最后一个，则插入所有的剩余数据
                if (i == targetList.size() - 1) {
                    // 插入剩余章节
                    for (int j = 0; j < sourceList.size(); j++) {
                        Integer chapter1 = sourceList.get(j);
                        returnList.add(chapter1);
                    }
                    break;
                }
                continue;
            }
        }
        return returnList;
    }
}
